package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.Codes;
import com.tianqu.tms.entity.TransferAcceptedInfo;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;
/**
 * 打印牌证申请表
 */
public class PLicenseSearchResultAction extends TmsAction {
	private Logger logger = Logger.getLogger(PLicenseSearchResultAction.class);

	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TransferRgActionForm inForm = (TransferRgActionForm) form;
		TransferRgInputViewHelper view = new TransferRgInputViewHelper();
        TmsUserProfile profile = uc.getUserProfile();
		Codes code = Codes.getInstance();
		TransferAcceptedInfo accept = new TransferAcceptedInfo();
        uc.setAttribute(TmsConstants.VIEWHELP, view);
		if (profile.getUserType() == 2 && accept.isSameMemberIdBetweenAttnAndCar(profile.getLoginId(), inForm.getSerial(), "") == false){
		    // 如果操作人类型是商户会员，即“当前操作人对应tms_user表的userType为：2，商户会员”根据车辆流水号（serial），搜索“过户业务基本信息表（tms_transfer_accepted_info）”，
		    // 如果过户业务基本信息表（tms_transfer_accepted_info）中的insertuserid在tms_attn中对应数据的memberid与当前操作人所属商户id（memberid）不符，
		    // 则提示“此车辆非您所在车行办理车辆，您无权处理此车。”
            setErrorsToRequest(request, TmsConstants.U12001 ,null);
//            return mapping.findForward(TmsConstants.SUCCESS);
            return mapping.getInputForward();
		}
        accept = accept.getAcceptInfoOnlyBySerial(inForm.getSerial(),"");
        if (accept != null) {

	        if (profile.getUserType() == 1 && accept.getConformityMark() == 2) {
	            // 如果操作人类型是操作人员，即“当前操作人对应tms_user表的userType为：1，操作人员”根据车辆流水号（serial），
	            // 搜索“过户业务基本信息表（tms_transfer_accepted_info）”中“变更表打印缴费状态（changeEngine）”。
	            // 如果“牌证表打印缴费状态（conformityMark）”为“2，未缴费”，则页提示“当前车辆未缴纳变更费用，请缴费后进行查询。”
	            setErrorsToRequest(request, TmsConstants.U12003 ,null);
//	            return mapping.findForward(TmsConstants.SUCCESS);
	            return mapping.getInputForward();
	        }

            /**/
			view.setSerial(inForm.getSerial());
			view.setStatus(code.getItemValueByItemIdAndItemCode(
					Codes.ACCEPT_STATUS, accept.getStatus()));// 过户状态
			view.setLicensePlate(accept.getLicensePlate());
			view.setVin(accept.getVin());

			view.setAgentName(accept.getAgentName());
			view.setAgentOwnerIdCardNo(accept.getAgentOwnerIdCardNo());
			view.setAgentAddress(accept.getAgentAddress());
			view.setAgentPhone(accept.getAgentPhone());

	        view.setModelCode(accept.getModelCode());
	        view.setVendorCode(accept.getVendorCode());
            if (accept.getCarUse() != null && "".equals(accept.getCarUse())) {
                view.setCarUse(Codes.getInstance().getItemValueByItemIdAndItemCode(Codes.USER_NATURE,Integer.parseInt(accept.getCarUse())));
            }
//		        view.setSendCardDate(accept.getSendCardDate());
	        view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,
	                accept.getOwnerType()));
	        view.setCarOwner(accept.getCarOwner());
	        view.setCarOwnerIdCardNo(accept.getCarOwnerIdCardNo());
	        view.setOwnerPhone(accept.getOwnerPhone());
	        view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,
	                accept.getCardType()));
	        view.setOwnerAddress(accept.getOwnerAddress());
//			// 开始往页面添加数据
//			if (accept.getMarketType() == 1) {
//				Car car = Car.getInstance();
//				car = car.findCarBySerial(accept.getBasicCarId());
//				if (car != null) {
//					view = toViewHelper(car, view);
//				}
//			}
//			if (accept.getMarketType() == 2) {
//				TransferPersonalCarInfo person = new TransferPersonalCarInfo();
//				person = person.getPersonalCarInfoBySerial(accept
//						.getBasicCarId());
//				if (person != null) {
//					view = toViewHelper1(person, view);
//				}
//			}
//			TransferProcessHistory his = new TransferProcessHistory();
//			view.setLcList(his.loadtHistoryBySerial(inForm.getSerial()));// 历史记录
		        
		} else {
			setErrorsToRequest(request, TmsConstants.IM006, null);
			return mapping.getInputForward();
		}
        view.setUserType(String.valueOf(profile.getUserType()));
		uc.setAttribute(TmsConstants.VIEWHELP, view);
		logger.debug(this.getClass().getName() + "#execute was compeleted.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
//
//	/**
//	 * entity -> viewhelper
//	 * 
//	 * @param entity
//	 * @return
//	 * @throws TmsDaoException
//	 */
//	private TransferRgInputViewHelper toViewHelper(Car entity,
//			TransferRgInputViewHelper view) throws TmsDaoException {
//
//		Codes code = Codes.getInstance();
//		view.setLicensePlate(entity.getLicensePlate());
//		view.setVin(entity.getVin());
//		view.setModelCode(entity.getModelCode());
//		view.setVendorCode(entity.getVendorCode());
//		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,
//				entity.getCarUse()));
//		view.setSendCardDate(entity.getSendCardDate());
//		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,
//				entity.getOwnerType()));
//		view.setCarOwner(entity.getCarOwner());
//		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
//		view.setOwnerPhone(entity.getOwnerPhone());
//		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,
//				entity.getCardType()));
//		view.setOwnerAddress(entity.getOwnerAddress());
//
//		return view;
//	}
//
//	/**
//	 * entity -> viewhelper
//	 * 
//	 * @param entity
//	 * @return
//	 * @throws TmsDaoException
//	 */
//	private TransferRgInputViewHelper toViewHelper1(
//			TransferPersonalCarInfo entity, TransferRgInputViewHelper view)
//			throws TmsDaoException {
//
//		Codes code = Codes.getInstance();
//		view.setLicensePlate(entity.getLicensePlate());
//		view.setVin(entity.getVin());
//		view.setModelCode(entity.getModelCode());
//		view.setVendorCode(entity.getVendorCode());
//		view.setCarUse(code.getItemValueByItemIdAndItemCode(Codes.USER_NATURE,
//				entity.getCarUse()));
//		view.setSendCardDate(entity.getSendCardDate());
//		view.setOwnerType(code.getItemValueByItemIdAndItemCode(Codes.CAR_OWNER,
//				entity.getOwnerType()));
//		view.setCarOwner(entity.getCarOwner());
//		view.setCarOwnerIdCardNo(entity.getCarOwnerIdCardNo());
//		view.setOwnerPhone(entity.getOwnerPhone());
//		view.setCardType(code.getItemValueByItemIdAndItemCode(Codes.PLATE_TYPE,
//				entity.getCardType()));
//		view.setOwnerAddress(entity.getOwnerAddress());
//		return view;
//	}
}
